$(function () {
    $("#jqGrid").Grid({
        url: '../cmsnews/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '标题', name: 'title', index: 'title', width: 80,align:'center'},
			{label: '简介', name: 'summary', index: 'summary', width: 80,align:'center'},
            {label: '类型', name: 'authorid', index: 'authorid', width: 80,align:'center',
                formatter:function (value) {
                    if(value==0){
                        return "新闻";
                    }else if(value==1){
                        return  "公告";
                    }
                }
            },
			{label: '作者名称', name: 'authorname', index: 'authorname', width: 80,align:'center'},
            {label: '状态', name: 'enabled', index: 'enabled', width: 80,align:'center',
                formatter: function (value) {
                console.log(value);
                    if (value == 1){
                        return "上架";
                     }
                    else if(value == 0){
                        return "下架";
                    }
                }
            },
			{label: '创建日期', name: 'createdat', index: 'createdAt', width: 80,align:'center',formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }
            /*,
            {name:'已经存在的列名称', label:'操作', width: 30,formatter:function(cellvalue, options, row){
                    return '<a href="#" onclick="vm.openSpe(\''+row.id+'\')">选择商品</a>';
                }
            }*/
        ]
    });
    $('#newdes').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })

});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		cmsNews: {},
		ruleValidate: {
            title: [
				{required: true, message: '标题不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		},
        // authors: [],//属性类别
        // categorys: []//属性类别
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.cmsNews = {};
            // vm.authors = [];
            // vm.categorys = [];
            $('#newdes').editable('setHTML', '');
            // vm.getAuthors();
            // vm.getCategorys();
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            // vm.getAuthors();
            // vm.getCategorys();
		},
        saveOrUpdate: function (event) {
            let url = vm.cmsNews.id == null ? "../cmsnews/save" : "../cmsnews/update";
            vm.cmsNews.newdes = $('#newdes').editable('getHTML');
            // vm.cmsNews.authorname = $('#authorselect option:selected').text();

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.cmsNews),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../cmsnews/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../cmsnews/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.cmsNews = r.cmsNews;
                    $('#newdes').editable('setHTML', vm.cmsNews.newdes);
                }
            });

		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        goodsreloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        // getAuthors: function () {
        //     Ajax.request({
        //         url: "../sportsuser/queryAll",
        //         async: true,
        //         successCallback: function (r) {
        //             vm.authors = r.list;
        //         }
        //     });
        // },
        // getCategorys: function () {
		// 	Ajax.request({
		// 		url: "../cmscategory/queryAll",
		// 		async: true,
		// 		successCallback: function (r) {
		// 			vm.categorys = r.list;
		// 		}
		// 	});
		// },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        openSpe: function (id) {

            openWindow({
                type: 2,
                title: '选择商品',
                content: '../shop/cmsnewgoods.html?type='+0+'&newsid=' + id
            })
        },
        handleSuccessPicUrl: function (res, file) {
            vm.cmsNews.imageUrl = file.response.url;
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.cmsNews.imageUrl;
            eyeImage(url);
        },

        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        }
	}
});